<?php
/**
 * Created by IntelliJ IDEA.
 * User: xingshi
 * Date: 16/9/1
 * Time: 下午2:34
 */

namespace App\Console\Commands;

use App\Utils;
use Illuminate\Console\Command;
use DB;
use App\User;
use App\Company;
use App\Job;
use App\Techlead;
use Auth;

//use Illuminate\Foundation\Inspiring;

class Insert extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'insert';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'build data';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        var_dump("begin:");

        $fi = file("/root/work/app/Console/Commands/nt.txt");
	$com_cnt=0;
	$tech_cnt=0;
	$job_cnt=0;
        foreach($fi as $k=>$line){
            $line = rtrim($line);
            $obj = json_decode($line);

            $com_ret = self::com($obj);
			if($com_ret["companyid"] == "no") continue;
			else if($com_ret["companyid"] == "") {
				$company = Company::create($com_ret["com_mp"]);
				$companyid = $company->id;
			}
            else $companyid = $com_ret["companyid"];

	$com_cnt+=1;
            $jobs = $obj->jobs;
            foreach($jobs as $job){
                $tech_ret = self::tech($job, $companyid);
				if($tech_ret["techleadid"] == "no") continue;
                else if($tech_ret["techleadid"] == "") $techleadid = Techlead::create($tech_ret["tech_mp"])->id;
                else $techleadid = $tech_ret["techleadid"];
				
				$tech_cnt+=1;

                $job_mp = self::jobb($job, $techleadid);
				if($job_mp == "no") continue;
                Job::create($job_mp);

				$job_cnt+=1;

var_dump($com_cnt."公司companyid:".$companyid."|".$com_ret["com_mp"]["name"]."----".$tech_cnt."主管|".$tech_ret["tech_mp"]["realname"]."----".$job_cnt."job|".$job_mp["title"]."********完成");
            }
        }

        var_dump("end");
    }

    private function tech($job, $companyid){
		if($job->referrerName==NULL||$job->referrerTitle==NULL)
            return array("techleadid"=> "no", "tech_mp"=>"");
        $tech_mp = array();
//        $tech_mp["userid"] = $job->;
        $tech_mp["realname"] = $job->referrerName;
        $tech_mp["title"] = $job->referrerTitle;
        $tech_mp["avatar_url"] = $job->referrerAvatarUrl;
        $tech_mp["companyid"] = $companyid;
//      $tech_mp["department"] = $job->;
        $tech_mp["isreal"] = 0;
        $tech_mp["isdelete"] = 0;

        $data = Techlead::whereRaw("realname = ?", [$job->referrerName])->get();
        if(count($data)>0){
            return array("techleadid"=> $data[0]->id, "tech_mp"=>$tech_mp);
        }
        else{
            return array("techleadid"=> "", "tech_mp"=>$tech_mp);
        }
    }

    private function jobb($job, $techleadid){
		if($job->title==NULL||$job->salary==NULL||$job->address==NULL||$job->description==NULL)
            return "no";
        $job_mp = array();
        $job_mp["techleadid"] = $techleadid;
//      $job_mp["taskid"] = ;
        $job_mp["title"] = $job->title;
        $job_mp["department"] = $job->apartment;
        $job_mp["salary"] = $job->salary;
        $job_mp["address"] = $job->address;
        $job_mp["desc"] = $job->description;
        $job_mp["isreal"] = 0;
        $job_mp["isdelete"] = 0;

        return $job_mp;
    }

    private function com($obj){
		if($obj->name==NULL || $obj->domain==NULL||$obj->size==NULL||$obj->logoUrl==NULL||$obj->foundation==NULL||$obj->location==NULL||$obj->industry==NULL||$obj->slogan==NULL||$obj->introduction==NULL) 
            return array("companyid"=> "no", "com_mp"=>"");

        $com_mp = array();
        $com_mp["name"] = $obj->name;
        $com_mp["domain"] = $obj->domain;
        $com_mp["size"] = $obj->size;
        $com_mp["avatar_url"] = $obj->logoUrl;
        $com_mp["foundation"] = $obj->foundation;
        $com_mp["location"] = $obj->location;
        $com_mp["industry"] = $obj->industry;
        $com_mp["slogan"] = $obj->slogan;
        $com_mp["introduction"] = $obj->introduction;
        //$com_mp["tags"] = $obj->tags;
        $com_mp["isreal"] = 0;
        $com_mp["isdelete"] = 0;
        $data = Company::whereRaw("name = ?", [$obj->name])->get();

        if(count($data)>0){
            return array("companyid"=> $data[0]->id, "com_mp"=>$com_mp);
        }
        else{
            return array("companyid"=> "", "com_mp"=>$com_mp);
        }
    }
}
